package com.wing.cabinet.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.UseInventoryInout;
import com.wing.cabinet.model.entity.UseInventoryUser;
import com.wing.cabinet.model.request.UseInventoryUserForm;
import com.wing.cabinet.model.response.TemporaryStorageExportVO;
import com.wing.cabinet.model.response.UseInventoryInoutVO;
import com.wing.cabinet.model.response.UseInventoryProductVO;
import com.wing.cabinet.model.response.UseInventoryUserVO;
import com.wing.cabinet.service.UseInventoryInoutDetailService;
import com.wing.cabinet.service.UseInventoryInoutService;
import com.wing.cabinet.service.UseInventoryUserService;
import com.wing.cabinet.utils.ExcelUtils;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.Product;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.DepartmentVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.excel.DownExcelUtil;
import com.wing.web.excel.ExcelUtil;
import com.wing.web.excel.UseInventoryUserExcelIn;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-用户产品在用库存管理（报失）", tags = "#/cabinet/useInventoryLoss")
@RestController("useInventoryLossController")
@RequestMapping("cabinet/admin/useInventoryLoss")
@RequiredArgsConstructor
@Slf4j
public class UseInventoryLossController {

    private final UseInventoryInoutService useInventoryInoutService;
    @Autowired
    private UserClient userClient;

    @ApiOperation(value = "查询所有售刀柜报失", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "存/取刀任务编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<UseInventoryInoutVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "id") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String code
            ,String cabCode
            ,String skuCode
            ,String oldSpecCode
            ,String deviceCode
            ,String startTime
            ,String endTime
            ,Long creatorId
            ,Long departmentId
            ,String query
            ,String mainCabCode
    ){

        Page<UseInventoryInout> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("code",code);
        queryParam.put("cabCode",cabCode);
        queryParam.put("skuCode",skuCode);
        queryParam.put("startCreateTime",startTime);
        queryParam.put("endCreateTime",endTime);
        queryParam.put("type",UseInventoryInout.Type.loss.getValue());
        queryParam.put("oldSpecCode",oldSpecCode);
        queryParam.put("deviceCode",deviceCode);
        queryParam.put("enterpriseCode", JwtUtils.getEnterpriseCode());
        queryParam.put("mainCabCode", mainCabCode);
        if (creatorId!=null){
            queryParam.put("creatorId",creatorId);
        }
        if (departmentId!=null&&creatorId==null){
            queryParam.put("departmentId",departmentId);
        }
        if (StringUtils.isNotBlank(query)){
            queryParam.put("query",query);
        }
        IPage<UseInventoryInoutVO> list = useInventoryInoutService.pageList4app(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
    @ApiOperation(value = "报失记录导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response, String startCreateTime, String endCreateTime, String enterpriseCode){
        List<UseInventoryInoutVO> list = new ArrayList<>();
        if ("".equals(startCreateTime) || "".equals(endCreateTime)){
            log.error("创建时间为空："+startCreateTime);
        }
        endCreateTime = endCreateTime+" 23:59:59";
        Map paramsMap = new HashMap();
        paramsMap.put("startCreateTime",startCreateTime);
        paramsMap.put("endCreateTime",endCreateTime);
        paramsMap.put("enterpriseCode",enterpriseCode);
        paramsMap.put("type",UseInventoryInout.Type.loss.getValue());
        list = useInventoryInoutService.list4app(paramsMap);
        for(UseInventoryInoutVO item : list){
            JsonResult<DepartmentVO> jsonResult = userClient.getDepartmentVOById(item.getDepartmentId());
            item.setCostCenter(jsonResult.getData().getCostCenter());
        }

        ExcelUtils.export(response,"报失记录",list, UseInventoryInoutVO.class);
    }
}
